package com.iplant.wms.controller.asn;

import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.ASNService;
import com.iplant.wms.service.po.asn.WMSReceiveBill;
import com.iplant.wms.service.po.asn.WMSReceiveBillItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/WMSReceiveBillItem")
public class WMSReceiveBillItemController extends BasicController<WMSReceiveBill> {


    @Autowired
    ASNService wASNService;

    public WMSReceiveBillItemController() {
        super(WMSReceiveBill.class);
    }


    /**
     * 用收货单主单据ID查子项集合
     *
     * @param request
     * @return
     */
    @GetMapping("/BillItemAll")
    public Object BillItemAll(HttpServletRequest request) {
        Object wResult = new Object();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wReceiveBillID = StringUtils.parseInt(request.getParameter("ReceiveBillID"), -1);
            Pagination wPagination = GetPagination(request, mClazz);

            ServiceResult<List<WMSReceiveBillItem>> wServiceResult = wASNService.WMS_QueryReceiveBillItemListByReceiveBillID(wLoginUser, wReceiveBillID, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, 0);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }


    @Override
    @TraceAPI("保存收货单子项")
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            WMSReceiveBillItem wItem = CloneTool.Clone(wParam.get("data"), WMSReceiveBillItem.class);

            ServiceResult<Integer> wServiceResult = wASNService.WMS_UpdateReceiveBillItem(wLoginUser, wItem);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }


    @Override
    @TraceAPI("批量保存收货单子项")
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            List<WMSReceiveBillItem> wItemList = CloneTool.CloneArray(wParam.get("data"), WMSReceiveBillItem.class);
            if (wItemList == null || wItemList.size()==0 ) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wASNService.WMS_UpdateReceiveBillItemList(wLoginUser, wItemList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }


    @Override
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            WMSReceiveBillItem wItem = CloneTool.Clone(wParam.get("data"), WMSReceiveBillItem.class);

            if (wItem == null || wItem.ID<=0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wASNService.WMS_DeleteReceiveBillItem(wLoginUser, wItem);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }
    @Override
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("data")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            List<WMSReceiveBillItem> wList = CloneTool.CloneArray(wParam.get("data"), WMSReceiveBillItem.class);

            if (wList == null || wList.size()==0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wASNService.WMS_DeleteReceiveBillItemList(wLoginUser, wList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }


    /**
     * 批量删除收货单子项（传IDs）
     *
     * @param request
     * @param wParam
     * @return
     */
    @PostMapping("/DeleteListByID")
    public Object ItemDeleteListByIDs(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();

        try {
            BMSEmployee wLoginUser = GetSession(request);

            if (!wParam.containsKey("ID")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            // 获取参数
            List<Integer> wIDList = StringUtils.parseIntList(wParam.get("ID"), ",");
            if (wIDList != null) {
                wIDList.removeIf(p -> p <= 0);
            }
            if (wIDList == null || wIDList.isEmpty()) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = wASNService.WMS_DeleteReceiveBillItemListByIDList(wLoginUser, wIDList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }

        return wResult;
    }



}
